package com.wn.service.doctorService;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.DepartmentCodeMapper;
import com.wn.mapper.DepartmentMapper;
import com.wn.mapper.MedicalAdviceMapper;
import com.wn.mapper.UserMapper;
import com.wn.ov.PatientOv;

import com.wn.pojo.Department;
import com.wn.pojo.DepartmentCode;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DoctorService {
    @Autowired
    private DepartmentCodeMapper departmentCodeMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据当前登录用户获得自己所属病人详情以及会诊病人详情
     */
    public PageInfo<PatientOv> getPatientById(Integer id, PageParam<PatientOv> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<PatientOv> ovList = userMapper.superSearch(pageParam.getSuperSearchKeyWord(), id);
            for (PatientOv patientOv : ovList) {
                if (patientOv.getStatus() == 1) {
                    patientOv.setStatu("住院中");
                } else if (patientOv.getStatus() == 0) {
                    patientOv.setStatu("未入院");
                }
            }

            PageInfo<PatientOv> ovPageInfo = new PageInfo<PatientOv>(ovList);
            return ovPageInfo;

        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<PatientOv> ovList = userMapper.getPatientById(id);
            for (PatientOv patientOv : ovList) {
                if (patientOv.getStatus() == 1) {
                    patientOv.setStatu("住院中");
                } else if (patientOv.getStatus() == 0) {
                    patientOv.setStatu("未入院");
                }
            }
            PageInfo<PatientOv> ovPageInfo = new PageInfo<PatientOv>(ovList);

            return ovPageInfo;
        }
    }

    /**
     * 病人转科审核
     */
    public ServerResponse checkTrans(DepartmentCode departmentCode) {
        DepartmentCode dc = departmentCodeMapper.selectByPrimaryKey(departmentCode.getId());
        if (dc.getStatus() == 0) {
            departmentCode.setStatus(1);
            departmentCodeMapper.updateByPrimaryKeySelective(departmentCode);
        } else if (dc.getStatus() == 1) {
            departmentCode.setStatus(0);
            departmentCodeMapper.updateByPrimaryKeySelective(departmentCode);
        } else {
            return new ServerResponse(ResponseCode.ERROR, "审核失败");
        }
        return new ServerResponse(ResponseCode.SUCCESS, "审核成功");
    }
}
